export let isObject = function(value) {
    return value !== null && typeof value === 'object';
};

export let isString = function (value) {return typeof value === 'string';};

export let isArray = function (arr) {
    if (Object.prototype.toString.apply(arr) === '[object Array]') return true;
    else return false;
};

export let isElement = function(node) {
    return !!(node &&
    (node.nodeName  // we are a direct element
    || (node.prop && node.attr && node.find)));  // we have an on and find method part of jQuery API
};

export let each = function (obj, callback) {
    if (typeof obj !== 'object') return;
    if (!callback) return;
    var i, prop;
    if (isArray(obj) ) {
        for (i = 0; i < obj.length; i++) {
            callback(i, obj[i]);
        }
    }
    else {
        for (prop in obj) {
            if (obj.hasOwnProperty(prop)) {
                callback(prop, obj[prop]);
            }
        }
    }
};

export let isEmpty = function (e) {
    if(isArray(e)){
        return e.length==0
    }else {
        var t;
        for(t in e)
            return !1;
        return !0
    }
};

export let oneOf = function  (value, validList) {
    for (let i = 0; i < validList.length; i++) {
        if (value === validList[i]) {
            return true;
        }
    }
    return false;
};

import iViewDataTimeUtil from '../components/iview/utils/date'
export let DateTime = iViewDataTimeUtil;
/**
 * 文件大小转换
 * @param limit file对象的size值
 * @returns {string}
 */
export const fileSizeCover = function(limit){
    if(limit){
        if(typeof limit=='string'){
            limit = new Number(limit);
        }
        var size = "";
        if( limit < 0.1 * 1024 ){ //如果小于0.1KB转化成B
            size = limit.toFixed(2) + "B";
        }else if(limit < 0.1 * 1024 * 1024 ){//如果小于0.1MB转化成KB
            size = (limit / 1024).toFixed(2) + "KB";
        }else if(limit < 0.1 * 1024 * 1024 * 1024){ //如果小于0.1GB转化成MB
            size = (limit / (1024 * 1024)).toFixed(2) + "MB";
        }else{ //其他转化成GB
            size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        }

        var sizestr = size + "";
        var len = sizestr.indexOf("\.");
        var dec = sizestr.substr(len + 1, 2);
        if(dec == "00"){//当小数点后为00时 去掉小数部分
            return sizestr.substring(0,len) + sizestr.substr(len + 3,2);
        }
        return sizestr;
    }else {
        return '0KB'
    }

};
